<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    .container {}

    .container .cart {
      width: 300px;
      margin: auto;
    }

    .container .title {
      background-color: lightblue;
      height: 40px;
      line-height: 40px;
      text-align: center;
      /*color: #fff;*/
    }

    .container .total {
      background-color: #FFCE46;
      height: 50px;
      line-height: 50px;
      text-align: right;
    }

    .container .total button {
      margin: 0 10px;
      background-color: #DC4C40;
      height: 35px;
      width: 80px;
      border: 0;
    }

    .container .total span {
      color: red;
      font-weight: bold;
    }

    .container .item {
      height: 55px;
      line-height: 55px;
      position: relative;
      border-top: 1px solid #ADD8E6;
    }

    .container .item img {
      width: 45px;
      height: 45px;
      margin: 5px;
    }

    .container .item .name {
      position: absolute;
      width: 90px;
      top: 0;
      left: 55px;
      font-size: 16px;
    }


    .container .item .price {
      position: absolute;
      width: 90px;
      top: 0;
      left: 110px;
      font-size: 16px
    }

    .container .item .change {
      width: 100px;
      position: absolute;
      top: 0;
      right: 50px;
    }

    .container .item .change a {
      font-size: 20px;
      width: 30px;
      text-decoration: none;
      background-color: lightgray;
      vertical-align: middle;
    }

    .container .item .change .num {
      width: 40px;
      height: 25px;
    }

    .container .item .del {
      position: absolute;
      top: 0;
      right: 0px;
      width: 40px;
      text-align: center;
      font-size: 40px;
      cursor: pointer;
      color: red;
    }

    .container .item .del:hover {
      background-color: orange;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="container">
      <cart></cart>

    </div>
  </div>


  <template id='cart_list'>

    <div>
      <div class="item" v-for='(item,index) in list'>
        <img :src="item.img" />
        <div class="name"> {{item.name}}</div>
        <div class="price"> {{item.price}} </div>
        <div class="change">


          <a href="" @click.prevent="subEvent(item,index)">－</a>
          <input type="text" class="num" :value="item.num" @change='changeNum(item,$event)' />
          <a href="" @click.prevent="addEvent(item,index)">＋</a>

          <!-- <a href=""  @click.prevent="changeEvent(item,index,'sub')">－</a>
          <input type="text" class="num"   :value="item.num"    @change="changeEvent('change')"/>
          <a href="" @click.prevent="changeEvent(item,index,'add')">＋</a> -->
        </div>
        <div class="del" @click='delData(index)'>×</div>
      </div>

    </div>

  </template>



  <template id='titles'>
    <h1 class="title"> {{title}}</h1>
  </template>

  <template id='total'>
    <div class="total"    v-if='list.length>0'>
      <span>总价格:  {{totalPrice}}</span>
      <button>结算</button>
    </div>
    <div v-else>
      <h1> 暂无商品  小姐姐 赶紧去添加 商品 我们现在有很大的优惠</h1>
    </div>
  </template>



  <script type="text/javascript" src="./vue.js"></script>
  <script type="text/javascript">
    let cartList = [
      {
        "img": "./img/a.jpg",
        "name": "TCL",
        "price": 2000,
        "num": 5
      },
      {
        "img": "./img/b.jpg",
        "name": "xiaomi",
        "price": 1999,
        "num": 1
      },
      {
        "img": "./img/c.jpg",
        "name": "Haier",
        "price": 1999,
        "num": 1
      },
      {
        "img": "./img/e.jpg",
        "name": "pptv",
        "price": 1999,
        "num": 1
      }
    ]

    // 定义三个基础组件 
    // 购物车商品标题组件
    var titles = {
      template: "#titles",
      props: ['title']

    }

    //购物车商品展示组件
    var cart_list = {
      template: "#cart_list",
      // 接受 cart 组件传递过来的数据
      props: ['list'],
      methods: {
        //删除功能  因为我们的数据是父组件传递过来的 所以我们遵循单向数据流 我们不能直接更改父组件传递过来的数据
        delData(index) {
          // alert(index)
          this.$emit('delGoods', index)
        },
        //商品加+1 
        addEvent(item, index) {
          let obj = {
            index,
            item
          }
          this.$emit('addGoods', obj)


        },
        //商品减 1
        subEvent(item, index) {
          let obj = {
            index,
            item
          }
          this.$emit('subGoods', obj)

        },
        //手动输入商品数量
        changeNum(item,event) {
          // console.log(event.target.value);
       
      
          let obj = { num: event.target.value*1, item }
          this.$emit('changeGoods', obj)

        }


      }


    }

    //购物车计算总价格组件
    var total = {
      template: '#total',
      props:['list'],

      computed:{

        totalPrice() {
            return this.list.reduce((cur,current)=>cur+current.price*current.num,0)
        },
      


      }

    }



    //定义最外层组件
    Vue.component('cart', {
      template: `
       <div>
            <titles  :title='title'></titles>
    
            <cart_list :list='list'  @delGoods='deleteGoods'  @addGoods='addCartGoods'   @subGoods='subCartGoods'   @changeGoods='changeCartGoods'    
            ></cart_list>
            <total  :list='list'></total>
        </div>
      `,
      data() {
        return {
          list: [],
          title: '购物车标题'

        }
      },
      methods: {
        //删除功能
        deleteGoods(i) {
          // alert(i)
          let currentIndex = this.list.findIndex((item, index) => index == i)
          this.list.splice(currentIndex, 1)

        },
        // 商品数量加1 
        addCartGoods(obj) {
          this.list.forEach(item => {
            if (item.name == obj.item.name) {
              item.num += 1
            }
          })
        },

        subCartGoods(obj) {
          this.list.forEach(item => {
            if (item.name == obj.item.name) {
              item.num -= 1

              if (item.num <= 0) {
                item.num = 1
                alert('不能再减了')

                return

              }
            }
          })
        },

        changeCartGoods(obj) {
          // console.log(obj);
          this.list.forEach(item=>{
            if(item.name==obj.item.name) {
              item.num=obj.num
            }
          })
        }



      },

      mounted() {
        this.list = cartList
      },

      components: {
        titles,
        cart_list,
        total
      }
    })



    var vm = new Vue({
      el: '#app',
      data: {




      },


    });

  </script>
</body>

</html>